
/**
 * @File    xxx
 * @brief   xxx
 * @auhtor  xxx
 * @data    xxx
 * @see     xxx
 * @note    xxx
**/

#include "server.h"
#include "msg_proc.h"
#include "proto_pack.h"

//////////////////////////////////////////////////////////////////////////////

int user_register_req(struct connect_t *conn, struct user_register_req_t *req)
{
    // 1, 插入用户信息表.
    // 2, 得到用户的自动生成ID.
    // 3, 插入用户登录表.
    // 4, 通过传入的结构,返回生成的用户ID.

    char sql_str[512];

    memset(sql_str, '\0', sizeof(sql_str));
    sprintf(sql_str, "insert into t_user_info values(null, '%s', '%s', %d, %d, %s, '%s', now())",
        req->user_info.name, req->user_info.nick, req->user_info.sex, req->user_info.age,
        req->user_info.birthday, req->user_info.note);
    printf("%s \n", sql_str);
    db_exec(sql_str);

    memset(sql_str, '\0', sizeof(sql_str));
    sprintf(sql_str, "select max(user_id) from t_user_info;");
    printf("%s \n", sql_str);
    char userid[32];
    int row, col;
    memset(userid, '\0', sizeof(userid));
    db_get_set(sql_str, &row, &col);
    db_get_field(userid, sizeof(userid)-1, 0, 1);
    db_clear_result();

    memset(sql_str, '\0', sizeof(sql_str));
    sprintf(sql_str, "insert into t_user_login values(null, %s, '%s', now(), now())",
        userid, req->user_info.email);
    printf("%s \n", sql_str);
    db_exec(sql_str);

    req->user_info.id = atoll(userid);

    return 0;
}

// 注册请求
int msg_user_register_req(struct connect_t *conn, struct proto_head_t *head, struct tlv_buf_t *buf)
{
    int retval = 0;
    struct user_register_req_t req;

    retval = user_register_req_unpack(buf, &req);
    if(retval == -1)
    {
        return -1;
    }

    retval = user_register_req(conn, &req);

    struct user_register_resp_t resp;
    resp.session = req.session;
    resp.result = retval;
    resp.id = req.user_info.id;
    tlv_buf_init(buf);
    user_register_resp_pack(buf, &resp);

    struct proto_head_t proto_head = {PROTO_MAGIC, PROTO_VER, 0, MSG_USER_REGISTER_ANSWER, 0};
    proto_head_insert(buf, &proto_head);

    // 添加到发送队列中.
    bufferevent_write(conn->buf_event, buf->data, buf->len);

    return 0;
}

//////////////////////////////////////////////////////////////////////////////

int user_login_req(struct connect_t *conn, struct user_login_req_t *req)
{
    char sql_str[512];

    memset(sql_str, '\0', sizeof(sql_str));
    sprintf(sql_str, "select userid from t_user_login where passwd='%s'",
        req->passwd);
    int row, col;
    db_get_set(sql_str, &row, &col);
    if(row == 0) {
        return -1;
    }

    return 0;
}

// 登录请求
int msg_user_login_req(struct connect_t *conn, struct proto_head_t *head, struct tlv_buf_t *buf)
{
    int retval = 0;
    struct user_login_req_t req;

    retval = user_login_req_unpack(buf, &req);
    if(retval == -1)
    {
        return -1;
    }

    retval = user_login_req(conn, &req);
    if(retval == -1) {
        return -1;
    }

    char sql_str[512];
    memset(sql_str, '\0', sizeof(sql_str));
    sprintf(sql_str, "select * from t_user_info where userid=%lld",
        req.id);
    int row, col;
    db_get_set(sql_str, &row, &col);
    struct user_login_resp_t resp;
    resp.id = req.id;
    resp.result = 0;

    db_get_field(resp.user_info.name, sizeof(resp.user_info.name)-1, 0, 2);
    db_get_field(resp.user_info.nick, sizeof(resp.user_info.nick)-1, 0, 3);

    char field[32];
    memset(field, '\0', sizeof(field));
    db_get_field(field, sizeof(field)-1, 0, 4);
    resp.user_info.sex = atoi(field);
    memset(field, '\0', sizeof(field));
    db_get_field(field, sizeof(field)-1, 0, 5);
    resp.user_info.age = atoi(field);

    db_get_field(resp.user_info.note, sizeof(resp.user_info.note)-1, 0, 7);

    tlv_buf_init(buf);
    user_login_resp_pack(buf, &resp);

    struct proto_head_t proto_head = {PROTO_MAGIC, PROTO_VER, 0, MSG_USER_LOGIN_RESP, 0};
    proto_head_insert(buf, &proto_head);

    // 添加到发送队列中.
    bufferevent_write(conn->buf_event, buf->data, buf->len);

    return 0;
}

//////////////////////////////////////////////////////////////////////////////

int msg_proc(struct connect_t *conn, struct proto_head_t *head, struct tlv_buf_t *buf)
{
    switch(head->type)
    {
    case MSG_USER_REGISTER_REQ:
        msg_user_register_req(conn, head, buf);
        break;
    case MSG_USER_LOGIN_REQ:
        msg_user_login_req(conn, head, buf);
        break;
    default:
        break;
    }

    return 0;
}

